//
//  WCG_O_ViewController.m
//  Project_B
//
//  Created by 黑黑 on 15/11/24.
//  Copyright © 2015年 Atom_Wang. All rights reserved.
//

#import "WCG_O_ViewController.h"

#import "WCG_O_TableViewCell.h"

#import "GXiaNetHandle.h"
#import "GXSpecialCategoryListModel.h"
#import "GXSpecialModel.h"

#import "WCG_O_DetailViewController.h"
#import "WCG_O_SectionHeaderViewController.h"
#import "GXSearchBarViewController.h"

#import "WCG_O_sectionHeaderView.h"
#import "MBProgressHUD.h"

@interface WCG_O_ViewController () <UITableViewDelegate, UITableViewDataSource, pushCollectionViewDelegate, WCG_O_sectionViewDelegate>

// 分类数组
@property (nonatomic, strong) NSMutableArray *categoryListArr;

@property (nonatomic, strong) UIButton *searchBtn;

@end

@implementation WCG_O_ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    // 导航栏设置
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.translucent = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    self.categoryListArr = [NSMutableArray array];
    [self createSearchBar];
    [self createTableView];
    [self getSpecialData];
    [self loadNightMBProgress];

}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    UIView *tabbarView = self.tabBarController.view.subviews[2];
    [UIView animateWithDuration:1 animations:^{
        tabbarView.frame = CGRectMake(0, kScreenHeight - 49, kScreenWidth, 49);
    }];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.translucent = NO;
    self.navigationController.navigationBarHidden = NO;
    NSLog(@"%@", self.navigationItem.titleView);

}


#pragma mark - 创建搜索框
- (void)createSearchBar {
    
    //创建搜索按钮
    self.searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    //设置按钮图像
    [self.searchBtn setImage:[[UIImage imageNamed:@"icon-search"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]forState:UIControlStateNormal];
    //设置标题
    [self.searchBtn setTitle:@"搜索内容和专题" forState:UIControlStateNormal];
    //设置字体颜色
    [self.searchBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    //设置字号
    [self.searchBtn.titleLabel setFont:[UIFont systemFontOfSize:17]];
    //设置位置
    [self.searchBtn setFrame:CGRectMake(0, 0, kScreenWidth, 40)];
    //设置为导航栏子视图
    self.navigationItem.titleView = self.searchBtn;
    //设置边框颜色
    self.searchBtn.layer.borderColor = [[UIColor blackColor] CGColor];
    //切割
    self.searchBtn.layer.masksToBounds = YES;
    //添加事件响应
    [self.searchBtn addTarget:self action:@selector(searchBtnActon) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark - search响应方法
- (void)searchBtnActon {
    GXSearchBarViewController *searchViewController = [[GXSearchBarViewController alloc] init];
    [self.tabBarController setHidesBottomBarWhenPushed:YES];
    self.tabBarController.tabBar.hidden = YES;
    [self.navigationController pushViewController:searchViewController animated:NO];
}

// 创建tableView
- (void)createTableView
{
    [self createTableViewWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 49 - 64)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.scrollsToTop = YES;
    [self.tableView registerClass:[WCG_O_TableViewCell class] forCellReuseIdentifier:@"cell"];
    // headerView
    
}
#pragma mark - tableView协议方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
        return self.categoryListArr.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    WCG_O_TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        cell.delegate = self;
        cell.specialArr = [[self.categoryListArr[indexPath.section] special_list] mutableCopy];
        return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - 请求数据
- (void)getSpecialData {

    //请求数据
    [GXiaNetHandle getUrl:kSpecialWeb cookie:kSpecialCookie success:^(id result) {
        //专题数据
        self.categoryListArr = [GXSpecialCategoryListModel transformWithArray:result[@"category_list"]];
        //将GXSpecialCategoryListModel中的数组套字典转换成数组套模型
        for (GXSpecialCategoryListModel *categoryModel in self.categoryListArr) {
            categoryModel.special_list = [GXSpecialModel transformWithArray:categoryModel.special_list];
        }
        if (self.categoryListArr.count != 0) {

            [self.tableView reloadData];
            [self removeHud];
            
        }
    } failure:^(NSError *error) {
        NSLog(@"%@", error);
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 64;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kScreenHeight - 64*3 - 49;
}

#pragma mark - 执行协议方法(collectionView的点击跳转方法)
- (void)pushCollectionViewWith:(NSNumber *)specialID
{
    WCG_O_DetailViewController *detailVC = [[WCG_O_DetailViewController alloc] init];
    detailVC.specialID = specialID;
    [self.navigationController pushViewController:detailVC animated:YES];
}

#pragma mark - section头部视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    //头部视图
    WCG_O_sectionHeaderView *headerView = [[WCG_O_sectionHeaderView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 64)];
    //设置代理
    headerView.delegate = self;
    //数据保护
    if (self.categoryListArr.count > section) {
        //赋值
        [headerView setCategoryModel:self.categoryListArr[section]];
        //返回视图
        return headerView;
    } else {
        return nil;
    }
}
#pragma mark - 实现点击头部视图的跳转方法
- (void)clickSectionView:(GXSpecialCategoryListModel *)catagoryModel
{
    WCG_O_SectionHeaderViewController *sectionVC = [[WCG_O_SectionHeaderViewController alloc] init];
    sectionVC.categoryModel = catagoryModel;
    [self.navigationController pushViewController:sectionVC animated:YES];
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
